//
//  BaseTabBarController.m
//  CharonTVProject
//
//  Created by karong on 2023/6/6.
//

#import "BaseTabBarController.h"
#import "BaseTabBar.h"
#import "BaseController.h"
#import "WSDiscoverController.h"
#import "WSTaskController.h"
#import "WSPlayerController.h"
#import "WSUserCenterController.h"
//Darwin.POSIX.ifaddrs

@interface BaseTabBarController ()<UITabBarControllerDelegate, BaseTabBarDelegate>

@property (nonatomic, strong) BaseTabBar *customTabBar;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
        
    BaseTabBar *tabBar = [[BaseTabBar alloc]init];
    self.customTabBar = tabBar;
    self.customTabBar.delegate = self;
    self.customTabBar.tabDelegate = self;
    [self setValue:tabBar forKey:@"tabBar"];
    
    NSMutableArray * controllers = [NSMutableArray new];
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"WSDiscoverController" bundle:nil];
    WSDiscoverController *discoverController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * discoverNav = [[BaseNavigationController alloc]initWithRootViewController:discoverController];
    discoverNav.navigationBarHidden = YES;
    [controllers addObject:discoverNav];
    
    storyBoard = [UIStoryboard storyboardWithName:@"WSPlayerController" bundle:nil];
    WSPlayerController * playerController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * playerNav = [[BaseNavigationController alloc]initWithRootViewController:playerController];
    playerNav.navigationBarHidden = YES;
    [controllers addObject:playerNav];
    
    
    storyBoard = [UIStoryboard storyboardWithName:@"WSTaskController" bundle:nil];
    WSTaskController *taskController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * taskNav = [[BaseNavigationController alloc]initWithRootViewController:taskController];
    taskNav.navigationBarHidden = YES;
    [controllers addObject:taskNav];

    storyBoard = [UIStoryboard storyboardWithName:@"WSUserCenterController" bundle:nil];
    WSUserCenterController * userCenterVC = [storyBoard instantiateInitialViewController];
    BaseNavigationController * userCenterNav = [[BaseNavigationController alloc]initWithRootViewController:userCenterVC];
    userCenterNav.navigationBarHidden = YES;
    [controllers addObject:userCenterNav];
    
    [self setViewControllers:controllers];
    [self setSelectedIndex:1];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [tabBar.items indexOfObject:item];
    if (index != self.selectIndex) {
        NSMutableArray * subView = [NSMutableArray new];
        for (UIView *tempView in tabBar.subviews) {
            if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                for (UIImageView *tempImageView in tempView.subviews) {
                    if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                        [subView addObject:tempImageView];
                    }
                }
            }
        }
        UIView * subItem = subView[index];
        [subItem addScaleAnimtaionWithfromValue:0.7 toValue:1.1 duration:0.15 repeatCount:1 autoreverses:YES];
        self.selectIndex = index;
    }
}




@end
